define(['jquery','common','markdown','bootstrap'], function($,common,markdown,bootstrap){
    //定义解析器
    var parser = new HyperDown();
    window.marked = function (text) {
        return parser.makeHtml(text);
    };
    //定义语言
    $.fn.markdown.messages.zh = {
        'Bold': "粗体",
        'Italic': "斜体",
        'Heading': "标题",
        'URL/Link': "链接",
        'Image': "图片",
        'List': "列表",
        'Unordered List': "无序列表",
        'Ordered List': "有序列表",
        'Code': "代码",
        'Quote': "引用",
        'Preview': "预览",
        'strong text': "粗体",
        'emphasized text': "强调",
        'heading text': "标题",
        'enter link description here': "输入链接说明",
        'Insert Hyperlink': "URL地址",
        'enter image description here': "输入图片说明",
        'Insert Image Hyperlink': "图片URL地址",
        'enter image title here': "在这里输入图片标题",
        'list text here': "这里是列表文本",
        'code text here': "这里输入代码",
        'quote here': "这里输入引用文本"
    };
    //定义编辑器
    var editor = {
        upload: '../../home/upload/image',
        init: function(element){
            element = element || '.editor-markdown';
            editor.upload = $(element).data('path');
            $(element).markdown({
                resize: 'vertical',
                language: 'zh',
                iconlibrary: 'fa',
                autofocus: false,
                savable: false,
                onShow: function(e){
                    //加载完成
                },
                additionalButtons: [
                    [{
                        name: "groupCustom",
                        data: [{
                            name: "cmdUploadImage",
                            toggle: false,
                            title: "上传图片",
                            icon: "bi-image",
                            callback: function (e) {
                                editor.uploadImage(e);
                                return false;
                            }
                        }]
                    }]
                ]
            });
        },
        uploadImage: function(e){
            var formData = new FormData();
            var $input = $('<input>', {
                'type': 'file',
                'name': 'file',
                'accept': 'image/*'
            }).on('change', function() {
                var file = this.files[0];
                if (file) {
                    formData.append('file', file);
                    $.ajax({
                        url: editor.upload,
                        type: 'POST',
                        data: formData,
                        processData: false,
                        contentType: false,
                        beforeSend: function(xhr){
                            editor.totast('<span class="spinner-border spinner-border-sm me-2"></span>上传中');
                        },
                        success: function(response) {
                            var imagePath = response.data.cdnUrl;
                            if(response.code==1){
                                var imageName = response.data.fileName;
                            }else{
                                var imageName = response.data.msg;
                            }
                            e.replaceSelection('!['+imageName+']('+imagePath+')'+"\n");
                        },
                        error: function(jqXHR, textStatus, errorThrown) {
                            console.log('上传失败:', textStatus);
                        },
                        complete: function(xhr,status){
                            editor.totastHide();
                        }
                    });
                }
            }).trigger('click');
        },
        totast: function(tips){
            $('body').append('<div id="editor-totast" class="toast-container position-fixed top-50 start-50 translate-middle"><div class="toast position-relative py-3 text-center border-0 bg-dark text-white" data-bs-delay="20000">'+tips+'</div></div>');
            new bootstrap.Toast($('#editor-totast .toast')).show();//hide()
        },
        totastHide:function(){
            $('#editor-totast').remove();
        }
    };
    //按格式返回
    return editor;
});